﻿@namespace Known.Components
@inherits BaseComponent
@typeparam TItem where TItem : class, new()

<AntTable @ref="table" TItem="TItem" Context="Context" Model="Model"
          ScrollX="@scrollX" ScrollY="@scrollY" Loading="isQuering"
          OnChange="OnChange" OnRow="OnRow"
          @bind-SelectedRows="Model.SelectedRows">
    @if (Model.SelectType != TableSelectType.None)
    {
        <Selection Type="@Model.SelectType.ToString().ToLower()" Align="ColumnAlign.Center" Width="40" Fixed="left" />
    }
    @if (Model.Columns != null && Model.Columns.Count > 0)
    {
        var isDictionary = Model.IsDictionary;

        @if (Model.ShowIndex)
        {
            var titleIndex = Language?.GetString("Index");
            var columnId = Model.Columns[0].Id;
            if (isDictionary)
            {
                @* <DictionaryColumn Title="@titleIndex" Property="c=>c.GetValue(columnId)" Align="ColumnAlign.Center" Width="50" Fixed="left">
                    <span>@(Model.Result.PageData.IndexOf(context) + 1 + (Model.Criteria.PageIndex - 1) * Model.Criteria.PageSize)</span>
                </DictionaryColumn> *@
            }
            else
            {
                <Column Title="@titleIndex" DataIndex="@columnId" TData="object" Align="ColumnAlign.Center" Width="50" Fixed="left">
                    <span>@(Model.Result.PageData.IndexOf(context) + 1 + (Model.Criteria.PageIndex - 1) * Model.Criteria.PageSize)</span>
                </Column>
            }
        }
        var columns = Context.GetUserTableColumns(Model);
        foreach (var item in columns)
        {
            if (!item.IsVisible)
                continue;

            var title = Language?.GetFieldName<TItem>(item);
            var width = item.Width > 0 ? item.Width.ToString() : "";
            RenderFragment<TItem> template = null;
            Model.Templates?.TryGetValue(item.Id, out template);
            if (isDictionary)
            {
                var value = (context as Dictionary<string, object>).GetValue(item.Id);
                <DictionaryColumn Title="@title" Property="c=>c.GetValue(item.Id)" Width="@width"
                                  Ellipsis ShowSorterTooltip="false" Fixed="@item.Fixed"
                                  Sortable="item.IsSort" DefaultSortOrder="item.ToSortDirection()"
                                  Align="@GetColumnAlign(item.Align)">
                    @if (template != null)
                    {
                        @template(context)
                    }
                    else if (item.Type == FieldType.Switch)
                    {
                        var isChecked = Utils.ConvertTo<bool>(value);
                        <Switch Disabled Value="isChecked" />
                    }
                    else
                    {
                        var text = GetColumnText(item, value);
                        if (item.IsViewLink)
                        {
                            <span class="kui-link" @onclick="e=>Model.ViewForm(context)">@text</span>
                        }
                        else
                        {
                            <span>@text</span>
                        }
                    }
                </DictionaryColumn>
            }
            else
            {
                var value = TypeHelper.GetPropertyValue(context, item.Id);
                <Column Title="@title" DataIndex="@item.Id" TData="object" Width="@width"
                        Ellipsis ShowSorterTooltip="false" Fixed="@item.Fixed"
                        Sortable="item.IsSort" DefaultSortOrder="item.ToSortDirection()"
                        Align="@GetColumnAlign(item.Align)">
                    <TitleTemplate>
                        @if (!string.IsNullOrWhiteSpace(item.Tooltip))
                        {
                            <Tooltip Title="@item.Tooltip">@title</Tooltip>
                        }
                        else
                        {
                            @title
                        }
                    </TitleTemplate>
                    <ChildContent>
                        @if (template != null)
                        {
                            @template(context)
                        }
                        else if (item.Type == FieldType.Switch)
                        {
                            var isChecked = Utils.ConvertTo<bool>(value);
                            <Switch Disabled Value="isChecked" />
                        }
                        else
                        {
                            var text = GetColumnText(item, value);
                            if (item.IsViewLink)
                            {
                                <span class="kui-link" @onclick="e=>Model.ViewForm(context)">@text</span>
                            }
                            else
                            {
                                <span>@text</span>
                            }
                        }
                    </ChildContent>
                </Column>
            }
        }
    }
    @if (Model.HasAction)
    {
        var actions = Model.RowActions?.Invoke(context);
        if (actions == null)
            actions = Model.Actions;
        Model.UpdateRowActions?.Invoke(context, actions);
        actions = actions?.Where(a => a.Visible).ToList();
        actions?.ForEach(d => d.Name = Language.GetString(d));
        <ActionColumn Title="@Language["Title.Action"]" Width="@Model.ActionWidth" Align="ColumnAlign.Center" Fixed="right">
            @if (actions.Count > Model.ActionCount)
            {
                var takes = actions.Skip(0).Take(Model.ActionCount - 1).ToList();
                var mores = actions.Skip(Model.ActionCount - 1).ToList();
                foreach (var item in takes)
                {
                    <Tag Color="@GetActionColor(item.Style)" OnClick="e=>Model.OnAction?.Invoke(item, context)">@item.Name</Tag>
                }
                var model = new DropdownModel
                {
                    Text = Language["Button.More"],
                    Items = mores,
                    OnItemClick = e =>
                    {
                        Model.OnAction?.Invoke(e, context);
                        return Task.CompletedTask;
                    }
                };
                <AntDropdown Context="Context" Model="model" />
            }
            else
            {
                foreach (var item in actions)
                {
                    <Tag Color="@GetActionColor(item.Style)" OnClick="e=>Model.OnAction?.Invoke(item, context)">@item.Name</Tag>
                }
            }
        </ActionColumn>
    }
    @if (Model.HasSum)
    {
        <SummaryRow>
            @if (Model.SelectType != TableSelectType.None)
            {
                <SummaryCell Class="kui-table-check" Align="ColumnAlign.Center">@Language["IsSum"]</SummaryCell>
            }
            @if (Model.Columns != null && Model.Columns.Count > 0)
            {
                foreach (var item in Model.Columns)
                {
                    if (!item.IsVisible)
                        continue;

                    if (item.IsSum)
                    {
                        object value = null;
                        Model.Result?.Statis?.TryGetValue(item.Id, out value);
                        <SummaryCell>@value</SummaryCell>
                    }
                    else
                    {
                        <SummaryCell />
                    }
                }
            }
            @if (Model.HasAction)
            {
                <SummaryCell />
            }
        </SummaryRow>
    }
</AntTable>